﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleTest
{
    class Program
    {
        private static void Main(string[] args)
        {
            int i=0 ;
            Func(i);
            Console.WriteLine(i.ToString());
            FuncRef(ref i);
            Console.WriteLine(i.ToString());
            FuncOut(out i);
            Console.WriteLine(i.ToString());
            Console.WriteLine("主线程启动");
            ThreadPool.QueueUserWorkItem(StartCode,5);
           new Task(StartCode, 6).Start();
            Console.WriteLine("主线程运行到此！");
            Thread.Sleep(1000);

            //            string[] messages = { "First task", "Second task",
            //"Third task", "Fourth task" };
            //            foreach (string msg in messages)
            //            {
            //                Task myTask = new Task(obj => printMessage((string)obj), msg);
            //                myTask.Start();
            //            }
            // wait for input before exiting
            Console.WriteLine("Main method complete. Press enter to finish.");
            Console.ReadLine();
        }
        private static void StartCode(object i)
        {
            Console.WriteLine("开始执行子线程...{0}", i);
            Thread.Sleep(1000);//模拟代码操作    
        }
        static void printMessage(string message)
        {
            Console.WriteLine("Message: {0}", message);
        }

        private static void Func(int i)
        {
            i = 3;
        }
        private static void FuncRef(ref int i)
        {
            i = 3;
        }

        private static void FuncOut(out int i)
        {
            i = 4;
        }
        //static void Main(string[] args)
        //{
        //    // create the cancellation token source
        //    CancellationTokenSource tokenSource = new CancellationTokenSource();

        //    // create the cancellation token
        //    CancellationToken token = tokenSource.Token;
        //    // create the task

        //    Task task = new Task(() =>
        //    {
        //        for (int i = 0; i < int.MaxValue; i++)
        //        {
        //            if (token.IsCancellationRequested)
        //            {
        //                Console.WriteLine("Task cancel detected");
        //                throw new OperationCanceledException(token);
        //            }
        //            else
        //            {
        //                Console.WriteLine("Int value {0}", i);
        //            }
        //        }
        //    }, token);

        //    // wait for input before we start the task
        //    Console.WriteLine("Press enter to start task");
        //    Console.WriteLine("Press enter again to cancel task");
        //    Console.ReadLine();

        //    // start the task
        //    task.Start();

        //    // read a line from the console.
        //    Console.ReadLine();

        //    // cancel the task
        //    Console.WriteLine("Cancelling task");
        //    tokenSource.Cancel();

        //    // wait for input before exiting
        //    Console.WriteLine("Main method complete. Press enter to finish.");
        //    Console.ReadLine();
        //}
    }
}
